﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PMSGSC.BLL;
using DevExpress.XtraEditors;
using PMSGSC.DAL;

namespace PMSGSC.App
{
    public partial class ucLoadEquipment : UserControl
    {
        public ucLoadEquipment()
        {
            InitializeComponent();
        }
        public bool isChanges;
        public void SetValueBeforeInsert(int id)
        {
            Equipment equip = Equipment_BLL.GetDataById(id);
            if (null != equip)
            {
                txtCode.Text = equip.CodeEquipment;
                txtName.Text = equip.NameEquipment;
                lbid.Text = equip.Equipment_ID.ToString();
            }
        }

        public void SetValue(string code, string name, string id)
        {
            txtCode.Text = code;
            txtName.Text = name;
            lbid.Text = id;
        }

        private void txtCode_Validating(object sender, CancelEventArgs e)
        {
            if (txtCode.Text != "   .   .   ")
            {
                Equipment equip = Equipment_BLL.GetDataByNumber(txtCode.Text);
                if (null == equip)
                {
                    XtraMessageBox.Show("Component not exits!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    e.Cancel = true;
                }
                else
                {
                    txtCode.Text = equip.CodeEquipment;
                    txtName.Text = equip.NameEquipment;
                    lbid.Text = equip.Equipment_ID.ToString();
                }
            }
            else
            {
                txtCode.Text = "";
                txtName.Text = "";
                lbid.Text = "";
            }
        }
        public string type;
        private void txtName_Validating(object sender, CancelEventArgs e)
        {
            if (txtName.Text != "")
            {
                if (txtName.Text[0] != '$' && txtName.Text[txtName.Text.Length - 1] != '$')
                {
                    Equipment equip = Equipment_BLL.GetDataByName(txtName.Text);
                    if (null == equip)
                    {
                        XtraMessageBox.Show("Component not exits!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        e.Cancel = true;
                    }
                    else
                    {
                        txtCode.Text = equip.CodeEquipment;
                        txtName.Text = equip.NameEquipment;
                        lbid.Text = equip.Equipment_ID.ToString();
                    }
                }
            }
            else
            {
                txtCode.Text = "";
                txtName.Text = "";
                lbid.Text = "";
            }
        }
        private void txtName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmGridComponent components = new frmGridComponent();
            components.ShowDialog();
            if (null != components.equip)
            {
                txtCode.Text = components.equip.CodeEquipment;
                txtName.Text = components.equip.NameEquipment;
                lbid.Text = components.equip.Equipment_ID.ToString();
                isChanges = true;
            }
        }
    }
}
